package com.lyb.redisspringboot.controller;

import com.lyb.redisspringboot.utils.JedisPoolUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author lyb
 * @create 2022/3/12-16:32
 * @method
 */
@Controller
public class seckillController {
    static String secKillScript ="local userid=KEYS[1];\r\n" +
            "local prodid=KEYS[2];\r\n" +
            "local qtkey='sk:'..prodid..\":qt\";\r\n" +
            "local userKey='sk:'..prodid..\":user\";\r\n" +
            "local userExists=redis.call(\"sismember\",userKey,userid);\r\n" +
            "if tonumber(userExists)==1 then \r\n" +
            "   return 2;\r\n" +
            "end\r\n" +
            "local num= redis.call(\"get\" ,qtkey);\r\n" +
            "if tonumber(num)<=0 then \r\n" +
            "   return 0;\r\n" +
            "else \r\n" +
            "   redis.call(\"decr\",qtkey);\r\n" +
            "   redis.call(\"sadd\",userKey,userid);\r\n" +
            "end\r\n" +
            "return 1" ;

    @GetMapping("/")
    public String index(){
        return "index";
    }

    @ResponseBody
    @GetMapping("/kill")
    /**
    public boolean seckill(HttpServletRequest request) throws IOException{
        String uid = (String) request.getAttribute("uid");
        String prodid = (String) request.getAttribute("prodid");
        // 1.uid 和 prodid 非空判断
        if(uid == null || prodid == null){
            System.out.println("uid = " + uid);
            System.out.println("prodid = " + prodid);
            return false;
        }

        // 2.连接redis，通过连接池
        JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
        Jedis jedis = jedisPoolInstance.getResource();

        // 3.拼接key
        String kcKey = "sk:" + prodid + ":qt";
        String userKey = "sk:" +prodid + ":user";

        // 监视库存，即使用乐观锁的方式
        jedis.watch(kcKey);

        // 4.判断库存是否为空，如果是空，则表示秒杀未开始
        String kc = jedis.get(kcKey);
        if (kc == null) {
            System.out.println("秒杀还没有开始，请等待");
            jedis.close();
            return false;
        }

        // 4.判断用户是否重复秒杀
        if (jedis.sismember(userKey, uid)) {
            System.out.println("已经秒杀成功，不能重复秒杀");
            jedis.close();
            return false;
        }

        // 5.判断商品数量，库存量小于1,秒杀结束
        if(Integer.parseInt(kc) <= 0){
            System.out.println("秒杀已经结束了");
            jedis.close();
            return false;
        }

        // 6.秒杀过程
        // 使用事务
        Transaction multi = jedis.multi();
        // 组队操作
        multi.decr(kcKey);
        multi.sadd(userKey, uid);
        // 执行
        List<Object> exec = multi.exec();
        if (exec == null || exec.size() == 0){
            System.out.println("秒杀失败了。。。。。。。");
            jedis.close();
            return false;
        }

        System.out.println("秒杀成功了");
        jedis.close();
        return true;
    }*/
    public boolean seckill(HttpServletRequest request){
        // 1.拼接key
        String uid = (String) request.getAttribute("uid");
        String prodid = (String) request.getAttribute("prodid");

        // 2.从连接池获取redis连接
        JedisPool jedispool =  JedisPoolUtil.getJedisPoolInstance();
        Jedis jedis=jedispool.getResource();

        // 3.加载并执行执行LUA脚本
        String sha1=  jedis.scriptLoad(secKillScript);
        Object result= jedis.evalsha(sha1, 2, uid,prodid);

        // 4.根据执行结果判断当前状态
        String reString=String.valueOf(result);
        if ("0".equals( reString )  ) {
            System.err.println("已抢空！！");
        }else if("1".equals( reString )  )  {
            System.out.println("抢购成功！！！！");
        }else if("2".equals( reString )  )  {
            System.err.println("该用户已抢过！！");
        }else{
            System.err.println("抢购异常！！");
        }
        jedis.close();
        return true;


    }

}
